package com.yanqu.road.server.cmd.activity.xingyoushanshui.cross;

import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogGoods;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserBagBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Date;
import java.util.Map;

@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_ADD_GOLD_COIN_FROM_CROSS,desc = "跨服来加金页子")
public class ShanShuiAddGoldCoinFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq req = XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq.parseFrom(yanQuMessage.getBytes());
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if(config != null){
            long playerId = yanQuMessage.getPlayerId();
            Object playerLock = GamePlayerMgr.getPlayerLock(playerId);
            LogGoods logGoods = null;
            UserInfo userInfo = UserMgr.getUserInfo(playerId);
            synchronized (playerLock){
                GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(playerId);
                if(onlinePlayer == null){
                    Map<Integer, UserBagItem> userBagItemMap = UserBagBussiness.getUserBagItemMap(playerId);
                    UserBagItem bagItem = userBagItemMap.get(config.getSHANSHUI_GOLD_COIN_ID());
                    if(bagItem == null) {
                        bagItem = new UserBagItem();
                        bagItem.setUserId(playerId);
                        bagItem.setGoodsId(config.getSHANSHUI_GOLD_COIN_ID());
                        bagItem.setNum(req.getNum());
                        bagItem.setModifyTime(System.currentTimeMillis() / 1000);
                        boolean result = UserBagBussiness.addUserBagItem(bagItem);
                        if (result) {
                            logGoods = new LogGoods(config.getSHANSHUI_GOLD_COIN_ID(), playerId, true, req.getNum(), bagItem.getNum(), req.getWay(), "", userInfo == null ? 0 : userInfo.getVipExp());
                        }
                    }else {
                        bagItem.setNum(bagItem.getNum() + req.getNum());
                        bagItem.setModifyTime(System.currentTimeMillis() / 1000);
                        boolean result = UserBagBussiness.updateUserBagItem(bagItem);
                        if(result){
                            logGoods = new LogGoods(config.getSHANSHUI_GOLD_COIN_ID(), playerId, true, req.getNum(), bagItem.getNum(), req.getWay(), "", userInfo == null ? 0 : userInfo.getVipExp());
                        }
                    }
                }else {
                    eLogMoneyType logMoneyType = eLogMoneyType.forValue(req.getWay());
                    onlinePlayer.getModule(CurrencyModule.class).addCurrency(config.getSHANSHUI_GOLD_COIN_ID() + "=" + req.getNum(), eLogMoneyType.XingYouShanShuiActivity, logMoneyType == null ? eLogMoneyType.XingYouShanShuiActivity : logMoneyType);
                }
                if(logGoods != null){
                    eLogMoneyType logMoneyType = eLogMoneyType.forValue(req.getWay());
                    LogMgr.addLogGoods(logGoods.getGoodsId(), logGoods.getUserId(), logGoods.getType(), logGoods.getNum(), logGoods.getNowNum(), logMoneyType == null ? eLogMoneyType.XingYouShanShuiActivity : logMoneyType, logGoods.getVipExp());
                }
            }
        }

    }
}
